package com.vm.osm.business.service.impl;

import com.vm.osm.business.dao.BaseDao;
import com.vm.osm.business.dao.OsmIndexConfigMapper;
import com.vm.osm.business.dao.OsmLogicNodeAttrMapper;
import com.vm.osm.business.entity.OsmIndexConfig;
import com.vm.osm.business.service.IOsmIndexConfigService;
import com.vm.osm.business.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by chenzui on 2018/4/19.
 */
@Service
public class OsmIndexConfigServiceImpl extends AbstractServiceImpl<OsmIndexConfig,String> implements IOsmIndexConfigService{

    private OsmIndexConfigMapper mapper;
    public OsmIndexConfigServiceImpl(OsmIndexConfigMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public String getIndex(String enterpriseId,String pri) {
        String result = "";
        OsmIndexConfig config = mapper.getNo(enterpriseId,pri);
        boolean flag = false;
        if(config == null){
            config = new OsmIndexConfig();
            config.setId(UuidUtil.getRandomUuid());
            config.setNo("1");
            config.setCreateTime(new Date());
            config.setModifiedTime(new Date());
            config.setPrix(pri);
            config.setEnterpriseId(enterpriseId);
            mapper.insertSelective(config);
            flag = true;
        }
        int no = 0;
        if(flag){
            no = 1;
        }else {
            no = Integer.valueOf(config.getNo());
            no++;
        }
        result = no+"";
        int length = config.getNo().length();
        for(int i=0;i<6-length;i++){
            result = "0"+result;
        }
        config.setNo(no+"");
        mapper.updateByPrimaryKeySelective(config);
        return pri+result;
    }
}
